Ext.onReady(function(){
    Ext.define('json_data',{
        extend: 'Ext.data.Model',
        fields: [
			 {name:'id',type:'string'},
             {name:'name',type:'string'},
             {name:'sex',type:'string'},
             {name:'birthday',type:'y年m月d日'},
             {name:'school',type:'string'},
             {name:'grade_id',type:'string'},
             {name:'grade__name',type:'string'},
             {name:'health',type:'string'},
             {name:'relationship',type:'int'},
             {name:'company_id',type:'string'},
             {name:'company__cn_name',type:'string'},
             // 监护人信息
             {name:'guardian_name',type:'string'},
             {name:'guardian_cardno',type:'string'},
             {name:'guardian_phone',type:'string'},
             {name:'guardian_addr',type:'string'},
             {name:'guardian_mobile',type:'string'},
             {name:'guardian_qq',type:'string'},
             // 学生入学前学习状况
             {name:'class_ranking',type:'string'},
             {name:'school_ranking',type:'string'},
             {name:'exam_time',type:'string'},
             {name:'yw',type:'string'},
             {name:'sx',type:'string'},
             {name:'yy',type:'string'},
             {name:'wl',type:'string'},
             {name:'hx',type:'string'},
             {name:'sw',type:'string'},
             {name:'ls',type:'string'},
             {name:'zz',type:'string'},
             {name:'dl',type:'string'},
             {name:'student_state',type:'string'},
             {name:'student_manager_id',type:'string'},
             {name:'student_manager__name',type:'string'},
             {name:'call_man_id',type:'string'},
             {name:'call_man__name',type:'string'},
             {name:'provide_man_id',type:'string'},
             {name:'provide_man__name',type:'string'},
             {name:'remark',type:'string'},
             
             // 回访表字段
             {name:'at_time',type:'string'},
             {name:'use_phone',type:'string'},
             {name:'student_id',type:'string'},
             {name:'student__name',type:'string'},
             {name:'parent_name',type:'string'},
             {name:'parent_phone',type:'string'},
             {name:'satisfaction',type:'string'},
             {name:'visit_man_id',type:'string'},
             {name:'visit_man__name',type:'string'},
             {name:'remark',type:'string'},
             
             // 课后跟踪表字段
             {name:'track_1',type:'string'},
             {name:'track_2',type:'string'},
             {name:'track_3',type:'string'}
        ]
    });
    //创建数据源
    var store = Ext.create('Ext.data.Store', {
    	pageSize:20,
        model: 'json_data',
	    proxy: {
	        type: 'ajax',
	        url: '/customercontrol/student_list/',
	        actionMethods:'post',
	        reader: {
	            type: 'json',
	            root: 'rows',
	            totalProperty: 'totalCount'
	        }
	    },
	    //groupField: 'student_state',
	    autoLoad: false
    });
    //创建数据源
    var storeVisit = Ext.create('Ext.data.Store', {
    	pageSize:20,
        model: 'json_data',
	    proxy: {
	        type: 'ajax',
	        url: '/customercontrol/student_visit_list/',
	        actionMethods:'post',
	        reader: {
	            type: 'json',
	            root: 'rows',
	            totalProperty: 'totalCount'
	        }
	    },
	    autoLoad: true
    });
    //创建数据源
    var storeTrack = Ext.create('Ext.data.Store', {
    	pageSize:20,
        model: 'json_data',
	    proxy: {
	        type: 'ajax',
	        url: '/customercontrol/student_track_list/',
	        actionMethods:'post',
	        reader: {
	            type: 'json',
	            root: 'rows',
	            totalProperty: 'totalCount'
	        }
	    },
	    autoLoad: true
    });
  	//年级combox
    var gradeComboBox= Ext.create('Ext.form.field.ComboBox', { 
	   name:'grade_id',
       fieldLabel: '年级', 
       editable:false,
       store: new Ext.data.Store({ 
           singleton : true, 
	   	   proxy: {
	   	        type: 'ajax',
	   	        url: '/customercontrol/grade_select/',
	   	        actionMethods:'post',
	   	        reader: {
	   	            type: 'json'
	   	        }
	   	    },
           fields:['id', 'name'],
           autoLoad:true 
       }), 
       loadingText:'正在加载数据，请稍侯……', 
       triggerAction:'all', 
       valueField:'id', 
       displayField:'name'
    }); 
  	//公司combox
    var companyComboBox= Ext.create('Ext.form.field.ComboBox', {
        id:'company_id1',
	   	name:'company_id',
       	fieldLabel: '校区', 
       	editable:false,
       	allowBlank : false,
       	emptyText: '请选择',
       	store: new Ext.data.Store({ 
           	singleton : true, 
	   	    proxy: {
	   	        type: 'ajax',
	   	        url: '/resourcecontrol/company_select_43/',
	   	        actionMethods:'post',
	   	        reader: {
	   	            type: 'json'
	   	        }
	   	    },
           fields:['id', 'cn_name'], 
           autoLoad:true 
       	}), 
       	loadingText:'正在加载数据，请稍侯……', 
       	triggerAction:'all', 
       	valueField:'id', 
       	displayField:'cn_name'
    });

    //学管师combox
    var employeComboBox= Ext.create('Ext.form.field.ComboBox', { 
       id : 'student_manager_id',
	   name:'student_manager_id',
       fieldLabel: '学管师', 
       typeAhead:true,
       minChars:1,
       editable:true,
       store: new Ext.data.Store({ 
           singleton : true, 
	   	   proxy: {
	   	        type: 'ajax',
	   	        url: '/employecontrol/employe_select30/',
	   	        actionMethods:'post',
	   	        reader: {
	   	            type: 'json'
	   	        }
	   	    },
           fields:['id', 'name'],
           autoLoad:true 
       }), 
       loadingText:'正在加载数据，请稍侯……', 
       triggerAction:'all', 
       valueField:'id', 
       displayField:'name',
       listeners: {
    		'select': function(combo, records, opts) {	
   	    		//alert("待增加学生查找") ;
    		},
    		'beforequery' : function(queryEvent,eOpts){
				queryEvent.combo.getStore().load();
			} 

    	}
    });
    //预约人combox
    var employeCallComboBox= Ext.create('Ext.form.field.ComboBox', { 
    	id : 'call_man_id',
    	name:'call_man_id',
    	fieldLabel: '<b><font color=red>预约人</font></b>', 
    	//emptyText: "【注意】参与预约提成",
    	typeAhead:true,
        minChars:1,
    	editable:true,
    	store: new Ext.data.Store({ 
    		singleton : true, 
    		proxy: {
    		type: 'ajax',
    		url: '/employecontrol/employe_select/',
    		actionMethods:'post',
    		reader: {
    		type: 'json'
    	}
    	},
    	fields:['id', 'name'],
    	autoLoad:true 
    	}), 
    	loadingText:'正在加载数据，请稍侯……', 
    	triggerAction:'all', 
    	valueField:'id', 
    	displayField:'name',
    	listeners: {
    		'select': function(combo, records, opts) {	
   	    		//alert("待增加学生查找") ;
    		},
    		'beforequery' : function(queryEvent,eOpts){
				queryEvent.combo.getStore().load();
			} 

    	}
    });
    //资源提供人combox
    var employeProvideComboBox= Ext.create('Ext.form.field.ComboBox', { 
    	id : 'provide_man_id',
    	name:'provide_man_id',
    	fieldLabel: '<b><font color=red>资源提供人</font></b>', 
    	//emptyText: "【注意】参与预约提成",
    	typeAhead:true,
        minChars:1,
    	editable:true,
    	store: new Ext.data.Store({ 
    		singleton : true, 
    		proxy: {
    		type: 'ajax',
    		url: '/employecontrol/employe_select/',
    		actionMethods:'post',
    		reader: {
    		type: 'json'
    	}
    	},
    	fields:['id', 'name'],
    	autoLoad:true 
    	}), 
    	loadingText:'正在加载数据，请稍侯……', 
    	triggerAction:'all', 
    	valueField:'id', 
    	displayField:'name',
    	listeners: {
    		'select': function(combo, records, opts) {	
   	    		//alert("待增加学生查找") ;
    		},
    		'beforequery' : function(queryEvent,eOpts){
				queryEvent.combo.getStore().load();
			} 

    	}
    });
    
    //合同状态combox
    var studentStateComboBox= Ext.create('Ext.form.field.ComboBox', { 
    	id:'student_state_combobox_id',
    	name:'student_state_id',
    	emptyText: '请选择学生状态',
    	labelWidth: 60,
    	width : 180,
    	fieldLabel: '学生状态', 
    	editable:false,
    	store: new Ext.data.Store({ 
			fields:['id','value'], 
			data:[
				{'id':'' ,'value':'全部'}, 
				{'id':'1','value':'已签'},
				{'id':'2','value':'未签有意向'},
				{'id':'3','value':'未签无意向'},
				{'id':'4','value':'其它'}
			],
	    	autoLoad:false 
    	}), 
    	loadingText:'正在加载数据，请稍侯……', 
    	triggerAction:'all', 
    	valueField:'id', 
    	displayField:'value'
    }); 
    
    
    //创建Grid
    var grid = Ext.create('Ext.grid.Panel',{
        store: store,
        //height: 500,
        //selModel: Ext.create('Ext.selection.CheckboxModel'),
//        features: [{
//            id: 'group',
//            ftype: 'groupingsummary',
//            groupHeaderTpl: '{name}',
//            hideGroupedHeader: true,
//            enableGroupingMenu: false
//        }],
        columnLines:true,
        columns: [
            Ext.create('Ext.grid.RowNumberer'),// 序列
			{text: "ID", width: 100, dataIndex: 'id', sortable: false,hidden : true
//				,
//	            summaryType: function(records){
//	                var total = 0;
//	                for (var i=0; i < records.length; ++i) {
//	                    record = records[i];
//	                    total += parseInt(record.get('id')) ;
//	                }
//	                return total;
//	            },
//	            summaryRenderer: Ext.util.Format.usMoney
            },
			{text: "校区id", width: 100, dataIndex: 'company_id', sortable: false,hidden : true},
			{text: "校区", width: 80, dataIndex: 'company__cn_name', sortable: false},
            {text: "姓名", width: 60, dataIndex: 'name', sortable: false},
            {text: "学管师ID", width: 100, dataIndex: 'student_manager_id', sortable: false,hidden : true},
            {text: "学管师", width: 60, dataIndex: 'student_manager__name', sortable: false},
            {text: "预约人ID", width: 100, dataIndex: 'call_man__id', sortable: false,hidden : true},
            {text: "预约人", width: 60, dataIndex: 'call_man__name', sortable: false},
            {text: "资源提供人ID", width: 100, dataIndex: 'provide_man__id', sortable: false,hidden : true},
            {text: "资源提供人", width: 65, dataIndex: 'provide_man__name', sortable: false},
            {text: "性别", width: 35, dataIndex: 'sex', sortable: false,
                renderer:function(v){
                	if (v==1) 
                    	return '男'  
        			else
            			return '女'
                }
            },
            {text: "出生日期", width: 80, dataIndex: 'birthday', sortable: false,hidden : true},
            {text: "学校名称", width: 120, dataIndex: 'school', sortable: false},
            {text: "年级ID", width: 80, dataIndex: 'grade_id', sortable: false,hidden : true},
            {text: "年级", width: 40, dataIndex: 'grade__name', sortable: false},
            {text: "健康状况", width: 120, dataIndex: 'health', sortable: false,hidden : true},
            {text: "监护人关系", width: 65, dataIndex: 'relationship', sortable: false,
            	 renderer:function(v){
            		if (v==1) 
                		return '父亲'  
    				else if(v==2)
        				return '母亲'
        			else
            			return '其它'
            	}
            },
            {text: "监护人姓名", width: 120, dataIndex: 'guardian_name', sortable: false},
            {text: "监护人身份证", width: 120, dataIndex: 'guardian_cardno', sortable: false,hidden : true},
            {text: "监护人家庭电话", width: 100, dataIndex: 'guardian_phone', sortable: false},
            {text: "监护人家庭地址", width: 120, dataIndex: 'guardian_addr', sortable: false,hidden : true},
            {text: "监护人手机", width: 80, dataIndex: 'guardian_mobile', sortable: false},
            {text: "监护人QQ", width: 120, dataIndex: 'guardian_qq', sortable: false,hidden : true},
            {text: "班级排名", width: 120, dataIndex: 'class_ranking', sortable: false,hidden : true},
            {text: "学校排名", width: 120, dataIndex: 'school_ranking', sortable: false,hidden : true},
            {text: "考试时间", width: 80, dataIndex: 'exam_time', sortable: false,hidden : true},
            {text: "语文", width: 120, dataIndex: 'yw', sortable: false,hidden : true},
            {text: "数学", width: 120, dataIndex: 'sx', sortable: false,hidden : true},
            {text: "英语", width: 120, dataIndex: 'yy', sortable: false,hidden : true},
            {text: "物理", width: 120, dataIndex: 'wl', sortable: false,hidden : true},
            {text: "化学", width: 120, dataIndex: 'hx', sortable: false,hidden : true},
            {text: "生物", width: 120, dataIndex: 'sw', sortable: false,hidden : true},
            {text: "历史", width: 120, dataIndex: 'ls', sortable: false,hidden : true},
            {text: "政治", width: 120, dataIndex: 'zz', sortable: false,hidden : true},
            {text: "地理", width: 120, dataIndex: 'dl', sortable: false,hidden : true},
            {text: "状态", width: 120, dataIndex: 'student_state', sortable: false,
            	renderer:function(v){
        			if (v==1) 
            			return '已签'  
					else if(v==2)
    					return '未签有意向'
    				else if(v==3)
        				return '未签无意向'
        			else
            			return '其它'
        		}
            },
            {text: "备注", width: 120, dataIndex: 'remark', sortable: false}
        ],
        title: '学生信息',
        // disableSelection: false,
        layout : 'fit',
        //frame:true,
        loadMask:true,
//        region: 'north',
//        renderTo: 'customer-tab',
        viewConfig: {
            stripeRows: true,
            listeners: {
                itemclick: function(dataview, record, item, index, e) {
    				gridVisit.enable() ;
    				gridTrack.enable() ;
                },
                itemdblclick: function(dataview, record, item, index, e) {
                	var selected = grid.getSelectionModel().getSelection();
    				form.getForm().reset();
					form.loadRecord(selected[0]);
        			win.show();
                }
            }
        },
        dockedItems : [
        	{
            xtype : 'pagingtoolbar',
            store : store,
            dock : 'bottom',
            displayInfo : true,
            //displayMsg: '显示 {0} - {1} 条，共计 {2} 条',
            autoScroll:true 
        	},
        	{
        	dock:'top',
        	xtype:'toolbar',
        	items:[{
        		xtype: 'combobox',
        		labelWidth: 30,
        		width:130,
        		id:'company_combox_id',
            	name:'company',
               	fieldLabel: '校区',
               	emptyText: '请选择校区',
               	editable:false,
               	store: new Ext.data.Store({
                   	singleton : true, 
        	   	    proxy: {
        	   	        type: 'ajax',
        	   	        url: '/resourcecontrol/company_select_43/',
        	   	        actionMethods:'post',
        	   	        reader: {
        	   	            type: 'json'
        	   	        }
        	   	    },
                   fields:['id', 'cn_name'], 
                   autoLoad:true 
               	}), 
               	loadingText:'正在加载数据，请稍侯……', 
               	triggerAction:'all', 
               	valueField:'id', 
               	displayField:'cn_name'        		
        	},studentStateComboBox,{
        		id:'name',
        		xtype:'textfield',
        		editable:true,
        		labelWidth: 30,
        		width:120,
        		fieldLabel:'姓名'
        	},{
        		xtype:'button',
        		text:'查询',
        		iconCls:'search', 
        		handler:function(){
        			if(Ext.getCmp('company_combox_id').getValue()!=null){
        				var params = {	name: Ext.getCmp('name').getValue(),
        								company_id: Ext.getCmp('company_combox_id').getValue(), 
        								state:Ext.getCmp('student_state_combobox_id').getValue()};
            		    Ext.apply(store.proxy.extraParams, params);
            		    store.currentPage=1;
            		    store.load();
        			}else{
        				Ext.Msg.alert('系统提示', '请选择校区!');
        			}
        		}
        	},{
        		xtype:'button',
        		text:'添加',
        		iconCls:'add', 
        		handler:function(){
        			form.getForm().reset();
        			win.show();
        		}
        	},{
        		xtype:'button',
        		text:'修改',
        		iconCls:'edit', 
        		handler:function(){
        			var selected = grid.getSelectionModel().getSelection();
	    			if(selected!=''){
	    				form.getForm().reset();
						form.loadRecord(selected[0]);
	        			win.show();
	    			}else{
	    				Ext.Msg.alert('系统提示', '请选择需要修改的学生!');
	    			}
        		}
        	},{
        		xtype:'button',
        		text:'删除',
        		iconCls:'delete', 
        		handler:function(){
        			var selected = grid.getSelectionModel().getSelection();
	    			if(selected!=''){
	    				Ext.MessageBox.confirm('提示', '确认删除吗?', function(btn){
	        				if(btn=='yes'){
	        					var ids="";
	                			Ext.each(selected, function (item) {
	                				ids+=item.data.id+",";
	                			});
	                			Ext.Ajax.request({
	                			    url:'/customercontrol/student_del/',
	                			    method : 'post',
	                			    params:{
	                			    	ids:ids
	                			    },
	                			    success:function(response){
	                			        var results =  Ext.decode(response.responseText);
	                			        if(results.success){
	                			        	store.load();
	                			        }else{
	                			        	Ext.Msg.alert('系统提示', results.message);
	                			        }
	                			    }
	                			});
	        				}
	        			});
	    			}else{
	    				Ext.Msg.alert('系统提示', '请选择需要删除的学生!');
	    			}
        		
        		}
        	}]
        }]
    });
    
    
    var form = Ext.create('Ext.form.Panel', {
		width: 820,
		//frame: true,
		bodyPadding: 5,
        fieldDefaults: {
            labelAlign: 'right',
            labelWidth: 90,
            msgTarget: 'side'
        },
        layout: 'anchor',
        defaults: {
            anchor: '100%'
        },
		items : [{
			xtype:'hiddenfield',
			fieldLabel:'id',
			name:'id'
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'textfield',
			items:[ {
				fieldLabel : '姓名',
				name : 'name',
				allowBlank : false,
				blankText : '请输入姓名'
			}, {
				xtype:'combobox',
				editable:false,
				valueField:'id',
				displayField:'value',
				queryMode:'local',
				fieldLabel : '性别',
				name : 'sex',
				allowBlank : false,
				blankText : '请输入性别',
				store:Ext.create('Ext.data.Store',{
					fields:['id','value'], 
					data:[
						{'id':'1','value':'男'},
						{'id':'2','value':'女'}
					]
				}),
				columnWidth: 1/2
			},{
				fieldLabel : '出生日期',
				name : 'birthday',
				allowBlank : false,
				editable:false,
				xtype:'datefield',
				format: 'Y-m-d',
				blankText : '请输入'
			}]
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'datefield',
			items:[ gradeComboBox, {
				fieldLabel : '学校名称',
				xtype: 'textfield',
				name : 'school', 
				allowBlank : true,
				blankText : '请输入'
			},{
				xtype:'combobox',
				editable:false,
				valueField:'id',
				displayField:'value',
				queryMode:'local',
				fieldLabel : '监护人关系',
				name : 'relationship',
				allowBlank : false,
				blankText : '请输入性别',
				store:Ext.create('Ext.data.Store',{
					fields:['id','value'], 
					data:[
						{'id':'1','value':'父亲'},
						{'id':'2','value':'母亲'},
						{'id':'3','value':'其它'}
					]
				}),
				columnWidth: 1/2
			}]
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'textfield',
			items:[ {
				fieldLabel : '监护人身份证',
				name : 'guardian_cardno',
				allowBlank : true,
				blankText : '请输入'
			}, {
				fieldLabel : '监护人家庭电话',
				name : 'guardian_phone', 
				allowBlank : true,
				blankText : '请输入'
			},{
				fieldLabel : '监护人姓名',
				name : 'guardian_name', 
				allowBlank : true,
				blankText : '请输入'
			}]
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'textfield',
			items:[ {
				fieldLabel : '监护人手机',
				name : 'guardian_mobile',
				allowBlank : true,
				blankText : '请输入'
			}, {
				fieldLabel : '监护人QQ',
				name : 'guardian_qq', 
				allowBlank : true,
				blankText : '请输入'
			},{
				fieldLabel : '班级排名',
				name : 'class_ranking',
				allowBlank : false,
				blankText : '请输入'
			}]
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'textfield',
			items:[ {
				fieldLabel : '学校排名',
				name : 'school_ranking', 
				allowBlank : true,
				blankText : '请输入'
			}, {
				fieldLabel : '考试时间',
				name : 'exam_time',
				xtype:'datefield',
				editable:false,
				format: 'Y-m-d',
				allowBlank : true,
				blankText : '请输入'
			},{
				xtype: 'textfield',
				fieldLabel : '语文',
				name : 'yw', 
				allowBlank : true,
				blankText : '请输入'
			}]
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'textfield',
			items:[ {
				fieldLabel : '数学',
				name : 'sx',
				allowBlank : true,
				blankText : '请输入'
			}, {
				fieldLabel : '英语',
				name : 'yy', 
				allowBlank : true,
				blankText : '请输入'
			},{
				fieldLabel : '物理',
				name : 'wl',
				allowBlank : true,
				blankText : '请输入'
			}]
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'textfield',
			items:[ {
				fieldLabel : '生物',
				name : 'sw',
				allowBlank : true,
				blankText : '请输入'
			}, {
				fieldLabel : '历史',
				name : 'ls', 
				allowBlank : true,
				blankText : '请输入'
			},{
				fieldLabel : '化学',
				name : 'hx', 
				allowBlank : true,
				blankText : '请输入'
			}]
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'textfield',
			items:[ {
				fieldLabel : '政治',
				name : 'zz',
				allowBlank : true,
				blankText : '请输入'
			}, {
				fieldLabel : '地理',
				name : 'dl', 
				allowBlank : true,
				blankText : '请输入'
			},companyComboBox]
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'textfield',
			items:[ {
				xtype:'combobox',
				editable:false,
				valueField:'id',
				displayField:'value',
				queryMode:'local',
				fieldLabel : '状态',
				name : 'student_state',
				allowBlank : false,
				blankText : '请输入',
				store:Ext.create('Ext.data.Store',{
					fields:['id','value'], 
					data:[
						{'id':'1','value':'已签'},
						{'id':'2','value':'未签有意向'},
						{'id':'3','value':'未签无意向'},
						{'id':'4','value':'其它'}
					]
				}),
				columnWidth: 1/2
			},employeComboBox]
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'textfield',
			items:[employeCallComboBox,employeProvideComboBox]
		},{
			xtype:'textfield',
			fieldLabel:'健康状况',
			name:'health'
		},/*{
			xtype:'textfield',
			fieldLabel:'监护人健康状况',
			name:'guardian_name'
		},*/{
			xtype:'textfield',
			fieldLabel:'监护人家庭地址',
			name:'guardian_addr'
		},{
			xtype:'textareafield',
			fieldLabel:'备注',
			name:'remark'
		}]
	});

	
	var win = Ext.create('Ext.window.Window', {
		title : '学生信息',
		modal: true,
		closeAction: 'hide',
		items : [form],
		plain : true,
		resizable : false,
		buttonAlign : 'center',
		buttons : [ {
			text : '保存',
			handler : function() {
				if (form.getForm().isValid()) {
					form.getForm().submit({
						waitTitle : '请稍候',
						waitMsg : '正在处理......',
						url : '/customercontrol/student_save/',
						success : function(form, action) {
							//Ext.Msg.alert('系统提示',  action.result.message);
							//form.getForm().reset();
							win.hide();
							store.load();
						},
						failure : function(form, action) {
							if (action.result) {
								Ext.Msg.alert('系统提示', action.result.message);
							} 
						}
					});
				}
			}
		}, {
			text : '取消',
			handler : function() {
				form.getForm().reset();
				win.hide();
			}
		} ]
	});
    

    
    
    //--
    //创建学生回访Grid
    var gridVisit = Ext.create('Ext.grid.Panel',{
    	disabled:true,
        store: storeVisit,
        //height: 500,
        //selModel: Ext.create('Ext.selection.CheckboxModel'),
        columnLines:true,
        columns: [
            Ext.create('Ext.grid.RowNumberer'),// 序列
			{text: "ID", width: 100, dataIndex: 'id', sortable: false,hidden : true},
			{text: "回访时间", width: 100, dataIndex: 'at_time', sortable: false},
			{text: "使用电话", width: 100, dataIndex: 'use_phone', sortable: false},
            {text: "回访学生ID", width: 100, dataIndex: 'student_id', sortable: false,hidden : true},
            {text: "回访学生", width: 120, dataIndex: 'student__name', sortable: false},
            {text: "家长姓名", width: 120, dataIndex: 'parent_name', sortable: false},
            {text: "家长电话", width: 120, dataIndex: 'parent_phone', sortable: false},
            {text: "满意度", width: 120, dataIndex: 'satisfaction', sortable: false,
            renderer:function(v){
                	if (v==1) 
                    	return '满意'
                    else if(v==2)
                    	return '较满意'
        			else 
            			return '不满意'
                }
			},
            {text: "回访人ID", width: 120, dataIndex: 'visit_man_id', sortable: false,hidden : true},
            {text: "回访人", width: 120, dataIndex: 'visit_man__name', sortable: false},
            {text: "备注", width: 120, dataIndex: 'remark', sortable: false}
        ],
        title: '回访信息',
        // disableSelection: false,
        layout : 'fit',
        //frame:true,
        loadMask:true,
//        region: 'north',
//        renderTo: 'customer-tab',
        viewConfig: {
            stripeRows: true,
            listeners: {
                itemclick: function(dataview, record, item, index, e) {
					
                },
                itemdblclick: function(dataview, record, item, index, e) {
                	var selected = gridVisit.getSelectionModel().getSelection();
					formVisit.getForm().reset();
					formVisit.loadRecord(selected[0]);
					winVisit.show();
                }
            }
        },
        dockedItems : [{
            xtype : 'pagingtoolbar',
            store : store,
            dock : 'bottom',
            displayInfo : true,
            //displayMsg: '显示 {0} - {1} 条，共计 {2} 条',
            autoScroll:true 
        },{
        	dock:'top',
        	xtype:'toolbar',
        	items:[/*{
        		xtype:'button',
        		text:'测试-需要修改为TAB激活',
        		handler:function(){
        			var selected = grid.getSelectionModel().getSelection();
        			var ids="";
        			Ext.each(selected, function (item) {
        				ids+=item.data.id;
        			});
        			//alert(ids) ;
        			Ext.getCmp('formVisit').getForm().setValues({student_id:ids});
        			
        			var params = {student_id: ids};
        		    Ext.apply(storeVisit.proxy.extraParams, params);
        		    storeVisit.load();
        		}
        	},*/{
        		xtype:'button',
        		text:'添加',
        		iconCls:'add', 
        		handler:function(){
        			formVisit.getForm().reset();
        			// 设置学生ID
        			var selected = grid.getSelectionModel().getSelection();
        			var ids="";
        			Ext.each(selected, function (item) {
        				ids+=item.data.id;
        			});
        			//alert(ids) ;
        			Ext.getCmp('formVisit').getForm().setValues({student_id:ids});
        			winVisit.show();
        		}
        	},{
        		xtype:'button',
        		text:'修改',
        		iconCls:'edit', 
        		handler:function(){
        			var selected = gridVisit.getSelectionModel().getSelection();
	    			if(selected!=''){
	    				formVisit.getForm().reset();
						formVisit.loadRecord(selected[0]);
	        			winVisit.show();
	    			}else{
	    				Ext.Msg.alert('系统提示', '请选择需要修改的回访信息!');
	    			}
        		}
        	},{
        		xtype:'button',
        		text:'删除',
        		iconCls:'delete', 
        		handler:function(){
        			var selected = gridVisit.getSelectionModel().getSelection();
	    			if(selected!=''){
	    				Ext.MessageBox.confirm('提示', '确认删除吗?', function(btn){
	        				if(btn=='yes'){
	        					var ids="";
	                			Ext.each(selected, function (item) {
	                				ids+=item.data.id+",";
	                			});
	                			Ext.Ajax.request({
	                			    url:'/customercontrol/student_visit_del/',
	                			    method : 'post',
	                			    params:{
	                			    	ids:ids
	                			    },
	                			    success:function(response){
	                			        var results =  Ext.decode(response.responseText);
	                			        if(results.success){
	                			        	storeVisit.load();
	                			        }else{
	                			        	Ext.Msg.alert('系统提示', results.message);
	                			        }
	                			    }
	                			});
	        				}
	        			});
	    			}else{
	    				Ext.Msg.alert('系统提示', '请选择需要删除的回访信息!');
	    			}
        		}
        	}]
        }]
    });
    
	var formVisit = Ext.create('Ext.form.Panel', {
		id:'formVisit',
		width: 350,
		//frame: true,
		bodyPadding: 5,
        fieldDefaults: {
            labelAlign: 'right',
            labelWidth: 90,
            msgTarget: 'side'
        },
        layout: 'anchor',
        defaults: {
            anchor: '100%'
        },
		items : [{
			xtype:'hiddenfield',
			fieldLabel:'id',
			name:'id'
		},{
			xtype:'datefield',
			fieldLabel:'回访时间',
			allowBlank : false,
			format: 'Y-m-d',
			name:'at_time'
		},{
			xtype:'textfield',
			fieldLabel:'使用电话',
			allowBlank : false,
			name:'use_phone'
		},{
			xtype:'hiddenfield',
			fieldLabel:'学生ID',
			allowBlank : false,
			name:'student_id'
		},{
			xtype:'textfield',
			fieldLabel:'家长姓名',
			allowBlank : false,
			name:'parent_name'
		},{
			xtype:'textfield',
			fieldLabel:'家长电话',
			allowBlank : false,
			name:'parent_phone'
		},{
				xtype:'combobox',
				editable:false,
				valueField:'id',
				displayField:'value',
				queryMode:'local',
				fieldLabel : '满意度',
				name : 'satisfaction',
				allowBlank : false,
				blankText : '请输入',
				store:Ext.create('Ext.data.Store',{
					fields:['id','value'], 
					data:[
						{'id':'1','value':'满意'},
						{'id':'2','value':'较满意'},
						{'id':'3','value':'不满意'}					
					]
				})
			},{
			xtype:'textareafield',
			fieldLabel:'备注',
			name:'remark'
		}]
	});

	
	var winVisit = Ext.create('Ext.window.Window', {
		title : '回访信息',
		modal: true,
		closeAction: 'hide',
		items : [formVisit],
		plain : true,
		resizable : false,
		buttonAlign : 'center',
		buttons : [ {
			text : '保存',
			handler : function() {
				if (formVisit.getForm().isValid()) {
					formVisit.getForm().submit({
						waitTitle : '请稍候',
						waitMsg : '正在处理......',
						url : '/customercontrol/student_visit_save/',
						success : function(form, action) {
							winVisit.hide();
							storeVisit.load();
						},
						failure : function(form, action) {
							if (action.result) {
								Ext.Msg.alert('系统提示', action.result.message);
							} 
						}
					});
				}
			}
		}, {
			text : '取消',
			handler : function() {
				formVisit.getForm().reset();
				winVisit.hide();
			}
		} ]
	});

	
	
	
	
	 //--
    //创建课后跟踪Grid
    var gridTrack = Ext.create('Ext.grid.Panel',{
    	disabled:true,
        store: storeTrack,
        //height: 500,
        //selModel: Ext.create('Ext.selection.CheckboxModel'),
        columnLines:true,
        columns: [
            Ext.create('Ext.grid.RowNumberer'),// 序列
			{text: "ID", width: 100, dataIndex: 'id', sortable: false,hidden : true},
			{text: "回访时间", width: 100, dataIndex: 'at_time', sortable: false},
            {text: "回访学生ID", width: 100, dataIndex: 'student_id', sortable: false,hidden : true},
            {text: "回访学生", width: 120, dataIndex: 'student__name', sortable: false},
            {text: "第一次", width: 120, dataIndex: 'track_1', sortable: false},
            {text: "第二次", width: 120, dataIndex: 'track_2', sortable: false},
            {text: "第三次", width: 120, dataIndex: 'track_3', sortable: false},
            {text: "备注", width: 120, dataIndex: 'remark', sortable: false}
        ],
        title: '课后跟踪',
        // disableSelection: false,
        layout : 'fit',
        //frame:true,
        loadMask:true,
//        region: 'north',
//        renderTo: 'customer-tab',
        viewConfig: {
            stripeRows: true,
            listeners: {
                itemclick: function(dataview, record, item, index, e) {
    				
                },
                itemdblclick: function(dataview, record, item, index, e) {
                	var selected = gridTrack.getSelectionModel().getSelection();
    				formTrack.getForm().reset();
					formTrack.loadRecord(selected[0]);
        			winTrack.show();
                }
            }
        },
        dockedItems : [{
            xtype : 'pagingtoolbar',
            store : store,
            dock : 'bottom',
            displayInfo : true,
            //displayMsg: '显示 {0} - {1} 条，共计 {2} 条',
            autoScroll:true 
        },{
        	dock:'top',
        	xtype:'toolbar',
        	items:[/*{
        		xtype:'button',
        		text:'测试-需要修改为TAB激活',
        		handler:function(){
        			var selected = grid.getSelectionModel().getSelection();
        			var ids="";
        			Ext.each(selected, function (item) {
        				ids+=item.data.id;
        			});
        			//alert(ids) ;
        			Ext.getCmp('formTrack').getForm().setValues({student_id:ids});
        			
        			var params = {student_id: ids};
        		    Ext.apply(storeTrack.proxy.extraParams, params);
        		    storeTrack.load();
        		}
        	},*/{
        		xtype:'button',
        		text:'添加',
        		iconCls:'add', 
        		handler:function(){
        			formTrack.getForm().reset();
        			// 设置学生ID
        			var selected = grid.getSelectionModel().getSelection();
        			var ids="";
        			Ext.each(selected, function (item) {
        				ids+=item.data.id;
        			});
        			//alert(ids) ;
        			Ext.getCmp('formTrack').getForm().setValues({student_id:ids});
        			winTrack.show();
        		}
        	},{
        		xtype:'button',
        		text:'修改',
        		iconCls:'edit', 
        		handler:function(){
        			var selected = gridTrack.getSelectionModel().getSelection();
	    			if(selected!=''){
	    				formTrack.getForm().reset();
						formTrack.loadRecord(selected[0]);
	        			winTrack.show();
	    			}else{
	    				Ext.Msg.alert('系统提示', '请选择需要修改的课后跟踪!');
	    			}
        		}
        	},{
        		xtype:'button',
        		text:'删除',
        		iconCls:'delete', 
        		handler:function(){
        			var selected = gridTrack.getSelectionModel().getSelection();
	    			if(selected!=''){
	    				Ext.MessageBox.confirm('提示', '确认删除吗?', function(btn){
	        				if(btn=='yes'){
	        					var ids="";
	                			Ext.each(selected, function (item) {
	                				ids+=item.data.id+",";
	                			});
	                			Ext.Ajax.request({
	                			    url:'/customercontrol/student_track_del/',
	                			    method : 'post',
	                			    params:{
	                			    	ids:ids
	                			    },
	                			    success:function(response){
	                			        var results =  Ext.decode(response.responseText);
	                			        if(results.success){
	                			        	storeTrack.load();
	                			        }else{
	                			        	Ext.Msg.alert('系统提示', results.message);
	                			        }
	                			    }
	                			});
	        				}
	        			});
	    			}else{
	    				Ext.Msg.alert('系统提示', '请选择需要删除的课后跟踪!');
	    			}
        		}
        	}]
        }]
    });
    
	var formTrack = Ext.create('Ext.form.Panel', {
		id:'formTrack',
		width: 350,
		//frame: true,
		bodyPadding: 5,
        fieldDefaults: {
            labelAlign: 'right',
            labelWidth: 90,
            msgTarget: 'side'
        },
        layout: 'anchor',
        defaults: {
            anchor: '100%'
        },
		items : [{
			xtype:'hiddenfield',
			fieldLabel:'id',
			name:'id'
		},{
			xtype:'datefield',
			fieldLabel:'回访时间',
			allowBlank : false,
			format: 'Y-m-d',
			name:'at_time'
		},{
			xtype:'hiddenfield',
			fieldLabel:'学生ID',
			allowBlank : false,
			name:'student_id'
		},{
			xtype:'textareafield',
			fieldLabel:'第一次',
			allowBlank : true,
			name:'track_1'
		},{
			xtype:'textareafield',
			fieldLabel:'第二次',
			allowBlank : true,
			name:'track_2'
		},{
			xtype:'textareafield',
			fieldLabel:'第三次',
			allowBlank : true,
			name:'track_3'
		},{
			xtype:'textareafield',
			fieldLabel:'备注',
			name:'remark'
		}]
	});

	
	var winTrack = Ext.create('Ext.window.Window', {
		title : '回访信息',
		modal: true,
		closeAction: 'hide',
		items : [formTrack],
		plain : true,
		resizable : false,
		buttonAlign : 'center',
		buttons : [ {
			text : '保存',
			handler : function() {
				if (formTrack.getForm().isValid()) {
					formTrack.getForm().submit({
						waitTitle : '请稍候',
						waitMsg : '正在处理......',
						url : '/customercontrol/student_track_save/',
						success : function(form, action) {
							winTrack.hide();
							storeTrack.load();
						},
						failure : function(form, action) {
							if (action.result) {
								Ext.Msg.alert('系统提示', action.result.message);
							} 
						}
					});
				}
			}
		}, {
			text : '取消',
			handler : function() {
				formTrack.getForm().reset();
				winTrack.hide();
			}
		} ]
	});
	
	
	

	Ext.create('Ext.container.Viewport',{
		layout:'fit',
		renderTo: 'customer-tab',
		items:[Ext.create('Ext.tab.Panel',{
			//renderTo: 'customer-tab',
			activeTab: 0,
			items:[grid,gridVisit,gridTrack],
			listeners:{
				'tabchange':function(tabPanel, newCard,oldCard,eOpts){
					if(newCard==gridVisit){
						var selected = grid.getSelectionModel().getSelection();
	        			var ids="";
	        			Ext.each(selected, function (item) {
	        				ids+=item.data.id;
	        			});
	        			Ext.getCmp('formVisit').getForm().setValues({student_id:ids});
	        			var params = {student_id: ids};
	        		    Ext.apply(storeVisit.proxy.extraParams, params);
	        		    storeVisit.load();
					}else if(newCard==gridTrack){
						var selected = grid.getSelectionModel().getSelection();
	        			var ids="";
	        			Ext.each(selected, function (item) {
	        				ids+=item.data.id;
	        			});
	        			Ext.getCmp('formTrack').getForm().setValues({student_id:ids});
	        			var params = {student_id: ids};
	        		    Ext.apply(storeTrack.proxy.extraParams, params);
	        		    storeTrack.load();
					}else{// 主grid
						gridVisit.disable() ;
						gridTrack.disable() ;
					}
				}
			}
		})]
	});
	
});